﻿using IcsSEMCtrl.Db;
using IcsSEMCtrl.Service.Logger;
using IcsSEMCtrl.Util.Client;
using Lifter.Device;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;

namespace Lifter.Gui.Monitor.Popups
{
    public partial class ModifyEqpConfigInfo : UIEditForm
    {
        private LifterSEM lftSem = null;
        public ModifyEqpConfigInfo()
        {
            InitializeComponent();
            if (this.lftSem == null)
            {
                this.lftSem = (LifterSEM)Naming.GetDeviceManager().GetDevice();
            }
            this.LoadcbNameData();
        }

        protected override bool CheckData()
        {
            return CheckEmpty(cbName, "请输入参数名")
                   && CheckEmpty(Value, "请输入参数值");
        }
        
        /// <summary>
        /// 加载物料ID控件数据
        /// </summary>
        private void LoadcbNameData()
        {
            this.cbName.Items.Clear();
            List<string> propertyNames = this.GetAllPropertyName();
            this.cbName.Items.AddRange(propertyNames.ToArray());
        }
        /// <summary>
        /// 获取所有EQP参数Name
        /// </summary>
        private List<string> GetAllPropertyName()
        {
            string sqlString = "SELECT NAME FROM EQUIPMENTUDPROPERTY";
            DBConnection dBConnection = DBControl.GetConnection();
            List<string> propertyDaos = new List<string>();
            SqlDataReader sqlDataReader = null;
            try
            {
                sqlDataReader = dBConnection.ExecuteQuery(sqlString);
                
                while (sqlDataReader.Read())
                {
                    string propertyName = sqlDataReader["NAME"].ToString();
                    propertyDaos.Add(propertyName);
                }            
            }
            catch (Exception ex)
            {
                LogHelper.loggerError("在获取所有EQP参数Name时报错", ex);
            }
            if (sqlDataReader != null)
            {
                sqlDataReader.Close();
            }
            DBControl.RemoveDBConection(dBConnection);
            return propertyDaos;
        }
        /// <summary>
        /// 更新EQP参数信息
        /// </summary>
        public void UpdateEQPInfo()
        {
            string infoName = this.cbName.Text;
            string infoValue = this.Value.Text;
            string infoDesc = this.tbDesc.Text;

            if (!string.IsNullOrEmpty(infoName))
            {
                string sqlString = $"UPDATE EQUIPMENTUDPROPERTY SET VALUE = '{infoValue}', DESCRIBE = N'{infoDesc}' WHERE NAME = '{infoName}'";
                DBConnection dBConnection = DBControl.GetConnection();
                try
                {
                    int returnCode = dBConnection.ExecuteUpdate(sqlString);
                    if (returnCode > 0)
                    {
                        this.ShowSuccessDialog("更新EQP参数信息成功，参数名字:" + infoName);
                        LogHelper.operationLogger($"用户{this.lftSem.UserName}更新了EQP参数，参数名为:{infoName}");
                    }
                    else
                    {
                        this.ShowWarningDialog("更新EQP参数信息失败，参数名字:" + infoName);
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.loggerError("更新EQP参数信息报错", ex);
                }
                DBControl.RemoveDBConection(dBConnection);
            }
        }
    }
}